package org.herrdommel.webpagechecker.pages;

import java.util.Calendar;

import org.herrdommel.webpagechecker.R;
import org.herrdommel.webpagechecker.util.DownloadPageTask;
import org.htmlcleaner.TagNode;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ControllingTUDFragment extends AbstractPage {

	public static String CONTROLLINGTUD = "Controlling TU Dortmund";
	private static String CONTROLLINGTUD_DOMAIN_ONE = "http://www.wiso.tu-dortmund.de/wiso/uc/de/lehre/ergeb/SS_2012/index.html";
	private static String CONTROLLINGTUD_DOMAIN_TWO = "http://www.wiso.tu-dortmund.de/wiso/uc/de/lehre/ergeb/SS_2012/ss_2012_ht/index.html";
	private static String CONTROLLINGTUD_PDF_LOCATION = "http://www.wiso.tu-dortmund.de/wiso/uc/de/lehre/ergeb/SS_2012/";

	private TagNode contentOne;
	private TagNode contentTwo;

	private TextView tvResult = null;
	private ProgressDialog pdLoadData = null;

	@Override
	public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
		setHasOptionsMenu(true);
		tvResult = new TextView(getActivity());
		tvResult.setGravity(Gravity.CENTER);
		tvResult.setMovementMethod(LinkMovementMethod.getInstance());
		final String cacheResult = checkCache(CONTROLLINGTUD, Calendar.DAY_OF_YEAR);
		if (cacheResult == null) {
			findResultsOne();
		} else {
			tvResult.setText(Html.fromHtml(cacheResult));
		}
		return tvResult;
	}

	@Override
	public void onRadarLoaded(final TagNode content) {
		switch (currentStep) {
			case STEP_ONE :
				contentOne = content;
				currentStep = STEP_TWO;
				findResultsTwo();
				break;
			case STEP_TWO :
				contentTwo = content;
				currentStep = STEP_FINISH;
				displayResults();
				break;
			default :
				if (pdLoadData.isShowing()) {
					pdLoadData.dismiss();
				}
				currentStep = STEP_FINISH;
				tvResult.setText(Html.fromHtml(getString(R.string.content_about)));
				break;
		}
	}

	private void findResultsOne() {
		currentStep = STEP_ONE;
		pdLoadData = ProgressDialog.show(getActivity(), getString(R.string.progress_message_loading),
				String.format(getString(R.string.progress_message_loading_data), CONTROLLINGTUD), true, true,
				new OnCancelListener() {

					@Override
					public void onCancel(final DialogInterface dialog) {
						currentStep = STEP_FINISH;
						tvResult.setText(R.string.content_loading_canceled);
						pdLoadData.cancel();
						pdLoadData.dismiss();
					}
				});
		new DownloadPageTask(this).execute(CONTROLLINGTUD_DOMAIN_ONE);
	}

	private void findResultsTwo() {
		new DownloadPageTask(this).execute(CONTROLLINGTUD_DOMAIN_TWO);
	}

	private void displayResults() {
		pdLoadData.setMessage(String.format(getString(R.string.progress_message_process_data), CONTROLLINGTUD));
		final StringBuffer sb = new StringBuffer();
		TagNode node = contentOne.findElementByAttValue("class", "wrapper", true, true);
		for (final TagNode aNode : node.getAllElements(true)) {
			if (aNode.getName().equals("h2")) {
				sb.append("<b><u>" + aNode.getText() + "</u></b><br />");
			} else if (aNode.getName().equals("li")) {
				sb.append("<a href=\"" + CONTROLLINGTUD_PDF_LOCATION + "/"
						+ ((TagNode) aNode.getChildren().get(1)).getAttributeByName("href") + "\">" + aNode.getText()
						+ "</a><br />");
			}
		}
		sb.append("<br />");

		node = contentTwo.findElementByAttValue("class", "wrapper", true, true);
		for (final TagNode aNode : node.getAllElements(true)) {
			if (aNode.getName().equals("h2")) {
				sb.append("<b><u>" + aNode.getText() + "</u></b><br />");
			} else if (aNode.getName().equals("li")) {
				sb.append("<a href=\"" + CONTROLLINGTUD_PDF_LOCATION + "/"
						+ ((TagNode) aNode.getChildren().get(1)).getAttributeByName("href") + "\">" + aNode.getText()
						+ "</a><br />");
			}
		}

		final String result = sb.toString();

		tvResult.setText(Html.fromHtml(result));
		pdLoadData.dismiss();
		saveToCache(CONTROLLINGTUD, result);
	}

	@Override
	public boolean onOptionsItemSelected(final MenuItem item) {
		switch (item.getItemId()) {
			case R.id.menu_reload :
				currentStep = STEP_INIT;
				findResultsOne();
				return true;

			default :
				break;
		}
		return super.onOptionsItemSelected(item);
	}

}
